/**
 * @file:          MT6835_21bit.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.06.09
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.06.09,18:31:37
 */
#ifndef _MT6835_21BIT_H_
#define _MT6835_21BIT_H_

/* Include Files **************************************************************/
#include "Datatype.h"
// #include "STC32G8K64.h"
#include "ch32v30x_module.h"
#include "encoder_cal_table.h"
#include "EncoderCalibrator.h"
#include "MT68xx.h"

/* Macro Definition ***********************************************************/
#define MT6835_PACKAGE_BYTES 4

/* Data Type Define ***********************************************************/
enum MT6835_Reg
{
    MT6835_REG_USER_ID = 0x001,
    MT6835_REG_NOT_USED = 0x002,
    MT6835_REG_ANGLE_20to13 = 0x003,
    MT6835_REG_ANGLE2_12to5 = 0x004,
    MT6835_REG_ANGLE3_4to0_STATUS = 0x005,
    MT6835_REG_CRC = 0x006,

    MT6835_REG_ABZ_RES_13to6 = 0x007,
    MT6835_REG_ABZ_RES_5to0__ABZ_OFF__AB_SWAP = 0x008,
    MT6835_REG_ZERO_POS_11to4 = 0x009,
    MT6835_REG_ZERO_POS_3to0__Z_EDGE__Z_PUL_WID = 0x00A,
    MT6835_REG_Z_PHASE_1to0__UVW_MUX__UVW_OFF__UVW_RES_3to0 = 0x00B,
    MT6835_REG_NLC_EN__PWM_FQ__PWM_POL__PWM_SEL_2to0 = 0x00C,
    MT6835_REG_ROT_DIR__HYST_2to0 = 0x00D,
    MT6835_REG_GPIO_DS__AUTOCAL_FREQ_2to0 = 0x00E,

    MT6835_REG_CLIBRATION_STATUS = 0x113,
};

enum MT6835_Cmd
{
    MT6835_CMD_READ_REG = 0x03 << 12,
    MT6835_CMD_WRITE_REG = 0x06 << 12,
    MT6835_CMD_WRITE_EEPROM = 0x0c << 12,
    MT6835_CMD_ZERO_OFFSET_TUNE = 0x05 << 12,
    MT6835_CMD_CONTINUE_READ = 0x0a << 12,
};

// enum MT6835_EEPROM_REG_VALUE
// {
//     MT6835_CMD_READ_REG = 0x03 << 12,
//     MT6835_CMD_WRITE_REG = 0x06 << 12,
//     MT6835_CMD_WRITE_EEPROM = 0x0c << 12,
//     MT6835_CMD_ZERO_OFFSET_TUNE = 0x05 << 12,
//     MT6835_CMD_CONTINUE_READ = 0x0a << 12,
// };

union MT6835_EncoderDataFrame
{
    uint32 all;
    uint8 u8data[4];
    struct
    {
        // uint32 crc : 8;
        // uint32 status : 3;
        uint32 angle : 16;
        uint32 reserve : 16;
    } bits;
};

// union MT6835_EncoderDataFrame
// {
//     uint32 all[2];
//     uint8 u8data[8];
//     struct
//     {
//         uint32 crc : 8;
//         uint32 status : 3;
//         uint32 angle : 21;
//         uint32 reserve : 32;
//     } bits;
// };

/* Variable Declare ***********************************************************/

/* Inline Function Define *****************************************************/

/* Function Declare ***********************************************************/
uint8 MT6835_21bit_Init(struct MT68xx *self);
int32 MT6835_21bit_SingleToMultiTurns(struct MT68xx *self);
int32 MT6835_21bit_GetEncoder(struct MT68xx *self);
int32 MT6835_21bit_EncoderRawRead(struct MT68xx *self);
int32 MT6835_21bit_GetEncoderCalibrated(struct MT68xx *self);
int32 MT6835_21bit_GetEncoderSingleturn(struct MT68xx *self);
uint8_t MT6835_21bit_GetEncoderCalibrationStatus(struct MT68xx *self);
uint16_t MT6835_21bit_WriteReg(struct MT68xx *self, uint16_t reg_addr, uint8_t *data_buff, uint8_t length);
uint16_t MT6835_21bit_ReadReg(struct MT68xx *self, uint16_t reg_addr, uint8_t *data_buff, uint8_t length);
uint16_t MT6835_21bit_SaveIntoEEPROM(struct MT68xx *self);
#endif